export default class SaveSystem {
    constructor(scene) {
        this.scene = scene;
        this.saveKey = 'cat_cafe_save';
        this.defaultData = {
            gold: 0,
            unlockedCats: ['cat_01'],
            achievements: []
        };
        this.load();
    }

    load() {
        try {
            const data = localStorage.getItem(this.saveKey);
            this.data = data ? JSON.parse(data) : {...this.defaultData};
            
            // 添加数据校验
            if (!this.validateData(this.data)) {
                this.data = {...this.defaultData};
            }
            
            this.scene.registry.merge(this.data);
        } catch (e) {
            console.error('加载存档失败:', e);
            this.data = {...this.defaultData};
        }
    }

    // 新增验证方法
    validateData(data) {
        return data && typeof data === 'object';
    }

    // 改用Cordova文件插件存储
    // 在save方法开头添加兼容处理
    save() {
        // 添加平台检测
        if (!window.cordova) {
            localStorage.setItem(this.saveKey, JSON.stringify(this.data));
            return;
        }
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory, dir => {
            dir.getFile('save.dat', { create: true }, fileEntry => {
                fileEntry.createWriter(writer => {
                    writer.write(JSON.stringify(this.data));
                });
            });
        });
    }

    updateFromRegistry() {
        this.data.gold = this.scene.registry.values.gold;
        // ...其他需要保存的数据
    }
}